package cn.fastoo.web.backstage.web.controller.sales;

import cn.fastoo.common.dao.sales.marketing.SalesMarketingActDao;
import cn.fastoo.common.dao.sales.marketing.SalesMarketingActMaterialDao;
import cn.fastoo.common.entity.sales.marketing.SalesMarketingAct;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.QuerySearch;
import cn.fastoo.common.sampleframe.search.Search;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Created by lowkey on 17/10/11.
 */
@Controller
@RequestMapping("/sale/marketing")
public class SalesMarketingController {

    @Autowired
    private SalesMarketingActDao salesMarketingActDao;
    @Autowired
    private SalesMarketingActMaterialDao salesMarketingActMaterialDao;

    @RequestMapping
    public String index() {
        return "/sales/marketing/index";
    }

    @RequestMapping("detail")
    public String marketing() {
        return "/sales/marketing/市场活动管理";
    }

    @RequestMapping("materialDetail")
    public String material() {
        return "/sales/marketing/物料申请表";
    }

    @RequestMapping("/manager")
    @ResponseBody
    public String manager(@ModelAttribute SalesMarketingAct newData, QuerySearch querySearch, String op) {

        if(StringUtils.isNotBlank(op)){
            if("add".equals(op)){
                salesMarketingActDao.insert(newData);
                //logger.info(loginUser, SystemLog.TYPE_AUTH_ROLE_ADD,String.format("用户角色[%s]",authRole.getRoleName()));
            }else {
                SalesMarketingAct oldData = salesMarketingActDao.findById(newData.getId());
                if(oldData==null){
                    return JsonResult.success("错误参数,请检查");
                }
                if("save".equals(op)){
                    salesMarketingActDao.update(oldData);
                }else if("del".equals(op)){
                    salesMarketingActDao.delete(oldData);
                }
            }
            return JsonResult.success("操作成功");
        }
        return JsonResult.querySearchSuccess(salesMarketingActDao.findPage(querySearch.getSearch()),querySearch);
    }

    //市场活动
    @RequestMapping("marketingInfo")
    @ResponseBody
    public String marketingInfo(@RequestParam Long id){
        return JsonResult.success(salesMarketingActDao.findById(id));
    }

    //物料申请表
    @RequestMapping("/info")
    @ResponseBody
    public String info(){
        return JsonResult.success(salesMarketingActMaterialDao.find(new Search()));
    }




}
